<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/4/13
 * Time: 16:34
 */

namespace app\admin\model;

use think\Db;
use think\Config;

class AppModel
{
    /*
         * 根据label status=1列表
         * */
    public function getAlbumByLabel($label){
        $model=Db::table('yui_album');
        return $model->where(['label'=>$label,'status'=>1])->order('updated_at desc')
            ->paginate(Config::get('app_page_info')['page_album_index'],false,[
                'type'     => 'bootstrap',
                'query' => [
                    'label' => $label
                ]
            ]);
    }

    /*
     * 获取相册每个分类最新的相片
     * */
    public function getAlbumNew(){
        $model=Db::table('yui_album')->alias('album')->field('labels.name,album.url,album.label,album.updated_at');
        $model=$model->join('yui_labels labels','album.label=labels.code');
        return $model->where(['album.status'=>1,'labels.status'=>1])->group('album.label')->order('album.updated_at desc')->select();
    }

    /*
     * 获取blog时间轴
     * */
    public function getTimeLine(){
        $model=Db::table('yui_motto');
        return $model->where('status',1)->order('created_at desc')
            ->paginate(5,false,[]);
    }

    /*
     * 根据id获取article文章详情
     * */
    public function getArticleById($id){
        $model=Db::table('yui_article');
        //浏览量++
        Db::table('yui_article')->startTrans();
        try{
            Db::table('yui_article')->where('id',$id)->setInc('read_num');
        }catch (\Exception $e) {
            Db::table('yui_article')->rollback();// 回滚事务
        }
        return $model->where('id',$id)->find();
    }

    /*
     * 根据db获取label
     * */
    public function getLabels($db){
        $model=Db::table('yui_labels');
        return $model->where(['status'=>1,'category'=>$db])->order('sort asc')->select();
    }

    /*
   * 根据db获取article
   * */
    public function getArticleByLabel($label){
        $model=Db::table('yui_article');
        return $model->where('label',$label)->order('updated_at desc')
            ->paginate(Config::get('app_page_info')['page_article_index'],false,[
                'type'     => 'bootstrap',
                'query' => [
                    'label' => $label
                ]
            ]);
    }

    /*
    * 根据db获取video
    * */
    public function getVideoByLabel($label){
        $model=Db::table('yui_video');
        return $model->where('label',$label)->order('updated_at desc')
            ->paginate(Config::get('app_page_info')['page_video_index'],false,[
                'type'     => 'bootstrap',
                'query' => [
                    'label' => $label
                ]
            ]);
    }

    /*
   * 根据id获取video详情
   * */
    public function getVideoById($id){
        $model=Db::table('yui_video');
        //浏览量++
        Db::table('yui_video')->startTrans();
        try{
            Db::table('yui_video')->where('id',$id)->setInc('read_num');
        }catch (\Exception $e) {
            Db::table('yui_video')->rollback();// 回滚事务
        }
        return $model->where('id',$id)->find();
    }
}